﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace devtm.Editor.TypeSelector.AttributeBuilder
{
    /// <summary>
    /// Interaction logic for TitleChooseAttributeUserControl.xaml
    /// </summary>
    public partial class TitleChooseAttributeUserControl : UserControl, INotifyPropertyChanged
    {
        public TitleChooseAttributeUserControl()
        {
            InitializeComponent();
            Liste.Visibility = System.Windows.Visibility.Collapsed;
            MyLabel.Visibility = System.Windows.Visibility.Visible;

            ImgSuppression.Visibility = System.Windows.Visibility.Collapsed;
            Liste.DataContext = this;
            MyLabel.DataContext = this;

        }

        #region Properties

        public ContextAttributeBuilderElement ContextAttributeBuilderElement { get; set; }

        public string Label { get { return ContextAttributeBuilderElement.SelectItem != null ? ContextAttributeBuilderElement.SelectItem.FullName : string.Empty; } }
        public Type SelectedItem
        {
            get { return ContextAttributeBuilderElement.SelectItem; }
            set
            {
                ContextAttributeBuilderElement.SelectItem = value;
                MyPropertyChanged("SelectedItem");
                MyPropertyChanged("Label");
            }
        }

        public new Object DataContext
        {
            get { return base.DataContext; }
            set
            {
                base.DataContext = value;
                Liste.ItemsSource = (IEnumerable<Type>)this.DataContext;
            }
        }

        #endregion

        #region Events

        private void ComboLostFocus(object sender, RoutedEventArgs e)
        {

            if (_NotAction > DateTime.Now)
            {
                Liste.IsDropDownOpen = true;
                return;
            }

            if (Liste.IsDropDownOpen)
                return;

            MyLabel.Visibility = System.Windows.Visibility.Visible;
            Liste.Visibility = System.Windows.Visibility.Collapsed;

        }


        #endregion

        #region INotifyPropertyChanged

        protected void MyPropertyChanged(PropertyChangedEventArgs e)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, e);
        }

        protected void MyPropertyChanged(string propertyName)
        {
            MyPropertyChanged(new PropertyChangedEventArgs(propertyName));
        }

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        private void LabelMouseDown(object sender, MouseButtonEventArgs e)
        {

            _NotAction = DateTime.Now.AddMilliseconds(500);

            Liste.Visibility = System.Windows.Visibility.Visible;
            MyLabel.Visibility = System.Windows.Visibility.Collapsed;
            MyImg.Visibility = System.Windows.Visibility.Collapsed;
            Liste.Focus();

        }

        private DateTime _NotAction;
        private void ComboSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            
            Liste.Visibility = System.Windows.Visibility.Collapsed;
            MyLabel.Visibility = System.Windows.Visibility.Visible;
            MyImg.Visibility = System.Windows.Visibility.Collapsed;

        }

        private void MyImg_MouseDown(object sender, MouseButtonEventArgs e)
        {
            

            _NotAction = DateTime.Now.AddMilliseconds(500);

            Liste.Visibility = System.Windows.Visibility.Visible;
            MyLabel.Visibility = System.Windows.Visibility.Collapsed;
            MyImg.Visibility = System.Windows.Visibility.Collapsed;

            if (ContextAttributeBuilderElement.Root.Count >1)
                ImgSuppression.Visibility = System.Windows.Visibility.Visible;

            Liste.Focus();

            ContextAttributeBuilderElement.Root.AddItem();

        }

        private void Suppress(object sender, MouseButtonEventArgs e)
        {
            ContextAttributeBuilderElement.Root.RemoveItem(ContextAttributeBuilderElement);
        }

    }
}
